<template>
  <div class="page-container">
    <div v-show="!addShow">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">

        <el-form-item label="结算对账类型" prop="settle_reconciliation_type">
          <el-select v-model="filters.settle_reconciliation_type" placeholder="结算对账类型" style="width:200px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in settleReconciliationTypeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="结算对象名称" prop="settle_reconciliation_name">
          <el-input v-model="filters.settle_reconciliation_name" placeholder="结算对象名称" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="结算单号" prop="settle_no">
          <el-input v-model="filters.settle_no" placeholder="结算单号" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="制单日期" prop="make_order_start_date">
          <el-date-picker
            v-model="filters.make_order_start_date"
            type="date"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            placeholder="开始" 
            class="input_postion"
            style="width:200px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="" prop="make_order_end_date">
          <el-date-picker
            v-model="filters.make_order_end_date"
            type="date"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            placeholder="结束" 
            class="input_postion"
            style="width:200px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="当前状态" prop="status" style="margin-left:28px;">
          <el-select v-model="filters.status" placeholder="当前状态" style="width:200px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in statusOption"
              :key="item.code"
              :label="item.label"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算币种" prop="currency_code" style="margin-left:28px">
          <el-select v-model="filters.currency_code" placeholder="结算币种" style="width:200px">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in currencyCodeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :width="1000"
      :height="250"
      :data="pageResult"
      :columns="filterColumns"
      :showOperation="false"
      :showEditOperation="false"
      :showBatchDelete="false"
      :showDeleteOperation="false"
      @findPage="findPage"
      ref="CyTable"
    >
    </cy-table>

    <!--新增-->
    <el-dialog
      :title="'新增'"
      width="30%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="120px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="结算对账类型" prop="settle_reconciliation_type">
          <el-select v-model="dataForm.settle_reconciliation_type" placeholder="结算对账类型">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in settleReconciliationTypeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算币种" prop="currency_code">
          <el-select v-model="dataForm.currency_code" placeholder="结算币种">
            <el-option label="请选择" value></el-option>
            <el-option
              v-for="item in currencyCodeOption"
              :key="item.code"
              :label="item.label"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button
          :size="size"
          type="primary"
          @click.native="nextStep"
        >下一步</el-button>
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
      </div>
    </el-dialog>

    <!--新增-->
    <el-dialog
      :title="'选择商家'"
      width="40%"
      :visible.sync="merchantVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row>
          <el-form-item  lable="商家编码" prop="merchant_code" style="float:left;padding-top:10px;padding-left:15px;" >
            <el-input v-model="filtersCyTable1.merchant_code" placeholder="商家编码"></el-input>
          </el-form-item>
          <el-form-item lable="商家名称" prop="merchant_name" style="float:left;padding-top:10px;padding-left:15px;">
            <el-input v-model="filtersCyTable1.merchant_name" placeholder="商家名称"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" style="float:right;"
                       @click="resetCyTable1"
                       type="primary"/>
            <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" style="float:right;"
                       @click="resetCyTable1"/>
          </el-form-item>
        </el-row>
        <!--表格内容栏-->
        <cy-table1 :height="1000"
                   :data="pageResult" :columns="clickColumns"
                   @findPage="clinkMock" ref="CyTable1"
                   :showBatchDelete="false" :showDeleteOperation=false
                   :showEditOperation=false
                   :showDetailOperation=false :showOperation="false">
        </cy-table1>
        <el-row></el-row>
        <el-row>
          <el-form-item>
            <kt-button :label="'下一步'" style="float:right;padding-top:10px;padding-left:15px;"
                       @click="nextStepMerchant"
                       type="primary"/>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
    </div>

    <div v-show="addShow">
      <addSettleBill @cancelClick="cancelClick"></addSettleBill>
    </div>

  </div>

</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import CyTable1 from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import addSettleBill from "@/views/Paje/addSettleBill";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

export default {
  components: {
    PopupTreeInput,
    CyTable,
    CyTable1,
    KtButton,
    TableColumnFilterDialog,
    addSettleBill
  },
  data() {
    return {
      size: "small",
      filters: {
        settle_reconciliation_type: "",
        settle_reconciliation_name: "",
        settle_no: "",
        make_order_start_date: "",
        make_order_end_date: "",
        status: "",
        currency_code: ""
      },
      settleReconciliationTypeOption: [{
        code: '1',
        label: '总部'
      },{
        code: '2',
        label: '供应商'
      }],
      currencyCodeOption: [{
        code: '1',
        label: 'CNY'
      },{
        code: '2',
        label: 'AUD'
      },{
        code: '3',
        label: 'GBP'
      },{
        code: '4',
        label: 'USD'
      }],
      statusOption: [{
        code: '1',
        label: '待确认'
      },{
        code: '2',
        label: '待审核'
      },{
        code: '3',
        label: '已审核待付款'
      },{
        code: '4',
        label: '审核不通过'
      },{
        code: '5',
        label: '已付款'
      }],
      radio: '1',
      options: {},
      filtersCyTable1: {
        merchant_code: "",
        merchant_name: ""
      },
      columns: [
        { prop: "settle_reconciliation_name", label: "结算对象名称", minWidth: 100 },
        { prop: "settle_reconciliation_type", label: "结算对象类型", minWidth: 100 },
        { prop: "settle_reconciliation_code", label: "结算对象编码", minWidth: 100 },
        { prop: "settle_no", label: "结算单号", minWidth: 100 },
        { prop: "currency_code", label: "结算币别", minWidth: 100 },
        { prop: "rate", label: "汇率", minWidth: 100 },
        { prop: "fee_must_amount", label: "费用应收金额", minWidth: 100 },
        { prop: "fee_must_pay_amount", label: "费用应付金额", minWidth: 100 },
        { prop: "finally_must_amount", label: "最终应付金额", minWidth: 100 },
        { prop: "goods_total_amount", label: "进退货总金额", minWidth: 100 },
        { prop: "already_amount", label: "已付金额", minWidth: 100 },
        { prop: "status", label: "当前状态", minWidth: 100 },
        { prop: "make_order_por", label: "制单人", minWidth: 100 },
        { prop: "make_order_time", label: "制单时间", minWidth: 100 },
        { prop: "review_por", label: "更新人", minWidth: 100 },
        { prop: "review_time", label: "更新时间", minWidth: 100 }
      ],
      clickColumns: [
        {prop: "merchantId", label: "商家编码", minWidth: 120},
        {prop: "merchantName", label: "商家名称", minWidth: 120},
        {prop: "merchantType", label: "商家类型", minWidth: 120},
      ],
      mockPage: {
        total: "3",
        rows: [{
          id:"1",
          settle_reconciliation_type: "供应商",
          settle_reconciliation_code: "1909170000040390",
          settle_reconciliation_name: "A供应商",
          settle_no: "1911220014128375",
          currency_code: "USD",
          rate: "",
          goods_total_amount: "168.00",
          fee_must_amount: "0.00",
          finally_must_amount: "168.00",
          fee_must_pay_amount: "0.00",
          already_amount: "0.00",
          status: "已审核待付款",
          make_order_por: "merchant",
          make_order_time: "2019-11-22 21:56:52",
          review_por: "merchant",
          review_time: "2019-11-22 21:56:52"
        }]
      },
      clickDetail: {
        "rows":
          [{
            "merchantId": "300001",
            "merchantName": "录屏商家二",
            "merchantType": "自营商家"
          },
            {
              "merchantId": "300001",
              "merchantName": "录屏商家二",
              "merchantType": "自营商家"
            }],
        "total": 1,
        "code": "0000"
      },

      filterColumns:[],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      dialogVisible: false,
      detailDialogVisible: false, // 新增编辑界面是否显示
      merchantVisible: false, //商家选择页
      addShow: false,
      dataForm: {
        settle_reconciliation_type: "",
        currency_code: ""
      },
      dataFormRules: {
        settle_reconciliation_type: [{ required: true, message: "请选择结算对象类型", trigger: "blur" }],
        currency_code: [{ required: true, message: "请选择结算币别", trigger: "blur" }]
      }
    };
  },
  methods: {
    cancelClick(){
      this.addShow  = !this.addShow;
    },
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPageMock(this.mockPage);
    },
    clinkMock: function () {
      this.$refs.CyTable1.findPageMock(this.clickDetail)
    },
    resetCyTable1: function () {
      this.filtersCyTable1 = {};
      this.clinkMock()
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
      }
    },
    // 显示新增界面
    handleAdd: function() {
      this.resetForm("dataForm");
      this.dialogVisible = true;
      this.operation = true;
    },
    // 下一步
    nextStep: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          var this_ = this;
          this_.dialogVisible = false;
          //只有选择供应商的时候才会弹出选择供应商页面
          if(this_.dataForm.settle_reconciliation_type == '2'){
            this_.merchantVisible = true;
          }else {
            this_.jumpPage();
          }
        }
      });
    },
    // 下一步(供应商)
    nextStepMerchant: function () {
      var this_ = this;
      this_.merchantVisible = false;
      this_.jumpPage();
    },
    //跳转新页
    jumpPage: function(){
      this.addShow = true;
    },

    // 批量删除
    handleDelete: function(data) {
      var this_ = this;
      this_.$message({message: "操作成功", type: "success"});
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "系统参数下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },


  mounted() {
	  this.initColumns();
  }

};
</script>

<style scoped>
</style>
